from utils.db_tools import *
from entity.modes import CheckLog, DateRecord
from entity.assemble import Assemble
from datetime import datetime
from loguru import logger as log


class DateRecordApi(object):

    @classmethod
    def query(cls, t_name):
        sql = "SELECT DATE_ID from date_record WHERE TABLE_FLAG='{}'".format(t_name)
        pdf = read_db(sql)
        return pdf['DATE_ID'].values.tolist()

    @classmethod
    def query_check_log(cls):
        return cls.query(CheckLog.__tablename__)

    @classmethod
    def add_check_log(cls, data: list, fmt="%Y-%m-%d"):
        t_name = CheckLog.__tablename__
        if len(data) > 0:
            log.info("考勤记录入库标示开始写入。。。。。")
            insert_batch(DateRecord,
                         [Assemble.date_record(t_name, d_id, datetime.strptime(d_id, fmt)) for d_id in data],
                         "DateRecord_err.json")
            log.info("写入结束")


if __name__ == '__main__':
    print(DateRecordApi.query("check_log"))
    da = ['2022-03-01', '2022-03-02', '2022-03-03', '2022-03-04', '2022-03-05', '2022-03-06', '2022-03-07',
          '2022-03-08', '2022-03-09', '2022-03-10', '2022-03-11', '2022-03-12', '2022-03-13', '2022-03-14',
          '2022-03-15', '2022-03-16', '2022-03-17', '2022-03-18', '2022-03-19', '2022-03-20', '2022-03-21',
          '2022-03-22', '2022-03-23', '2022-03-24', '2022-03-25', '2022-03-26', '2022-03-27', '2022-03-28',
          '2022-03-29']
    DateRecordApi.add_check_log(da)
